﻿/*  ------------------------------------------------------------------------
	 NQ Common Services Library
	 
	 Homepage: http://www.awzhome.de/
	 ------------------------------------------------------------------------
	 
	 The contents of this file are subject to the Mozilla Public License
	 Version 1.1 (the "License"); you may not use this file except in
	 compliance with the License. You may obtain a copy of the License at
	 http://www.mozilla.org/MPL/
	 
	 Software distributed under the License is distributed on an "AS IS"
	 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	 License for the specific language governing rights and limitations
	 under the License.
	
	 The Original Code is code of NQ Common Services Library.

	 The Initial Developer of the Original Code is Andreas Weizel.
	 Portions created by the Initial Developer are
	 Copyright (C) 2011 Andreas Weizel. All Rights Reserved.
	 
	 Contributor(s): (none)
	 
	 Alternatively, the contents of this file may be used under the terms
	 of the GNU General Public License Version 2.0 or later (the "GPL"),
	 or the GNU Lesser General Public License Version 2.1 or later (the
	 "LGPL"), in which case the provisions of the GPL or the LGPL are
	 applicable instead of those above. If you wish to allow use of your
	 version of this file only under the terms of the GPL or the LGPL and
	 not to allow others to use your version of this file under the MPL,
	 indicate your decision by deleting the provisions above and replace
	 them with the notice and other provisions required by the GPL or the
	 LGPL. If you do not delete the provisions above, a recipient may use
	 your version of this file under the terms of any of the MPL, the GPL
	 or the LGPL.
	 ------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Xml.Linq;
using AWZhome.NQ.Core.Fluent;
using AWZhome.NQ.Core;
using System.IO;
using System.Xml;
using AWZhome.NQ.CommonServices.Fluent;

namespace AWZhome.NQ.CommonServices.Tests
{
	[TestFixture]
	public class ConfigServiceFluentIfcTest
	{
		private NQServiceManagerImpl _servManager;
		private string _globalConfigFile;
		private string _userConfigFile;

		public ConfigServiceFluentIfcTest()
		{
			_globalConfigFile = Path.Combine(Path.GetTempPath(), "NQTestConf_Global.xml");
			_userConfigFile = Path.Combine(Path.GetTempPath(), "NQTestConf_User.xml");
		}

		[SetUp]
		public void SetupManager()
		{
			// Initializing of NQ service manager, enforcing only the NQCS basic component
			_servManager = new NQServiceManagerImpl(new string[] {
				"-ca", "NQCommServBasic.Component.dll",
				"-cf", "NQCommServ.Component.dll",
				"-sfg", _globalConfigFile,
				"-sfu", _userConfigFile}, NQHostMode.Console);
			NQServiceManager.SetImplementation(_servManager);

			// Load components and services
			_servManager.LoadComponents();
			_servManager.LoadServices();
		}

		[TearDown]
		public void DestroyManager()
		{
			if (_servManager != null)
			{
				_servManager.DestroyServices();
			}

			// Delete test files
			if (File.Exists(_globalConfigFile))
			{
				File.Delete(_globalConfigFile);
			}
			if (File.Exists(_userConfigFile))
			{
				File.Delete(_userConfigFile);
			}
		}

		[Test]
		public void GetSetting_MustReturnNullIfNotFound()
		{
			// Prepare test file
			XElement root = new XElement("NQConfiguration");
			XDocument doc = new XDocument(root);
			root.SetAttributeValue("version", "1.0");
			XElement categoryNode = new XElement("category");
			categoryNode.SetAttributeValue("name", "testcat");
			root.Add(categoryNode);
			XElement groupNode = new XElement("group");
			groupNode.SetAttributeValue("name", "testgroup");
			categoryNode.Add(groupNode);
			XElement valueNode = new XElement("value");
			valueNode.SetAttributeValue("name", "testval");
			valueNode.Value = "A_Value";
			groupNode.Add(valueNode);
			doc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			Assert.That(
				Configuration
					.Value("testval")
					.OfCategory("testcat_____")
					.InGroup("testgroup")
					.Get
				, Is.Null);
			Assert.That(
				Configuration
					.Value("testval")
					.OfCategory("testcat")
					.InGroup("testgroup_____")
					.Get
				, Is.Null);
			Assert.That(
				Configuration
					.Value("testval_____")
					.OfCategory("testcat")
					.InGroup("testgroup")
					.Get
				, Is.Null);
		}

		[Test]
		public void GetSetting_MustReadFromGlobalXML()
		{
			// Prepare test file
			XElement root = new XElement("NQConfiguration");
			XDocument doc = new XDocument(root);
			root.SetAttributeValue("version", "1.0");
			XElement categoryNode = new XElement("category");
			categoryNode.SetAttributeValue("name", "testcat");
			root.Add(categoryNode);
			XElement groupNode = new XElement("group");
			groupNode.SetAttributeValue("name", "testgroup");
			categoryNode.Add(groupNode);
			XElement valueNode = new XElement("value");
			valueNode.SetAttributeValue("name", "testval");
			valueNode.Value = "A_Value";
			groupNode.Add(valueNode);
			doc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			Assert.AreEqual("A_Value",
				(string) Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").Get.AsString());
		}

		[Test]
		public void GetSetting_MustReadFromUserXML()
		{
			// Prepare test file
			XElement root = new XElement("NQConfiguration");
			XDocument doc = new XDocument(root);
			root.SetAttributeValue("version", "1.0");
			XElement categoryNode = new XElement("category");
			categoryNode.SetAttributeValue("name", "testcat");
			root.Add(categoryNode);
			XElement groupNode = new XElement("group");
			groupNode.SetAttributeValue("name", "testgroup");
			categoryNode.Add(groupNode);
			XElement valueNode = new XElement("value");
			valueNode.SetAttributeValue("name", "testval");
			valueNode.Value = "A_Value";
			groupNode.Add(valueNode);
			doc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			Assert.AreEqual("A_Value",
				(string) Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").Get.AsString());
		}

		[Test]
		public void GetSetting_MustReadFromMergedXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			gvalueNode.Value = "A_Value";
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "testval");
			uvalueNode.Value = "userTESTVAL";
			ugroupNode.Add(uvalueNode);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			Assert.AreEqual("userTESTVAL",
				(string) Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").Get.AsString());
		}

		[Test]
		public void GetListSetting_MustReturnNullIfNotFound()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.Value = "List1";
			glistNode.Add(gvalueNode1);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.Value = "List2";
			glistNode.Add(gvalueNode2);
			XElement gvalueNode3 = new XElement("value");
			gvalueNode3.Value = "List3";
			glistNode.Add(gvalueNode3);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Test return values, if category, group or setting are not found
			IList<ConfigData> list = Configuration.List("testlist").OfCategory("testcat____").InGroup("testgroup").Get;
			Assert.That(list, Is.Null);
			list = Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup____").Get;
			Assert.That(list, Is.Null);
			list = Configuration.List("testlist____").OfCategory("testcat").InGroup("testgroup").Get;
			Assert.That(list, Is.Null);
		}

		[Test]
		public void GetListSetting_MustReadFromGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.Value = "List1";
			glistNode.Add(gvalueNode1);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.Value = "List2";
			glistNode.Add(gvalueNode2);
			XElement gvalueNode3 = new XElement("value");
			gvalueNode3.Value = "List3";
			glistNode.Add(gvalueNode3);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			IList<ConfigData> list = Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup").Get;
			Assert.AreEqual("List1", list[0].AsString());
			Assert.AreEqual("List2", list[1].AsString());
			Assert.AreEqual("List3", list[2].AsString());
		}

		[Test]
		public void GetListSetting_MustReadFromUserXML()
		{
			// Prepare test files
			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement ulistNode = new XElement("list");
			ulistNode.SetAttributeValue("name", "testlist");
			ugroupNode.Add(ulistNode);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.Value = "userList1";
			ulistNode.Add(uvalueNode1);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.Value = "userList2";
			ulistNode.Add(uvalueNode2);
			XElement uvalueNode3 = new XElement("value");
			uvalueNode3.Value = "userList3";
			ulistNode.Add(uvalueNode3);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			IList<ConfigData> list = Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup").Get;
			Assert.AreEqual("userList1", list[0].AsString());
			Assert.AreEqual("userList2", list[1].AsString());
			Assert.AreEqual("userList3", list[2].AsString());
		}

		[Test]
		public void GetListSetting_MustReadFromMergedXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.Value = "List1";
			glistNode.Add(gvalueNode1);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.Value = "List2";
			glistNode.Add(gvalueNode2);
			XElement gvalueNode3 = new XElement("value");
			gvalueNode3.Value = "List3";
			glistNode.Add(gvalueNode3);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement ulistNode = new XElement("list");
			ulistNode.SetAttributeValue("name", "testlist");
			ugroupNode.Add(ulistNode);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.Value = "userList1";
			ulistNode.Add(uvalueNode1);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.Value = "userList2";
			ulistNode.Add(uvalueNode2);
			XElement uvalueNode3 = new XElement("value");
			uvalueNode3.Value = "userList3";
			ulistNode.Add(uvalueNode3);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test, if we can read the value
			IList<ConfigData> list = Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup").Get;
			Assert.AreEqual("userList1", list[0].AsString());
			Assert.AreEqual("userList2", list[1].AsString());
			Assert.AreEqual("userList3", list[2].AsString());
		}

		[Test]
		public void SetSetting_MustSaveInGlobalXML()
		{
			// Load configuration
			Configuration.Load();

			// Save the setting
			Assert.That(
				Configuration
					.Value("testval")
					.OfCategory("testcat")
					.InGroup("testgroup")
					.SetGlobal(new ConfigData().FromString("testVALUE"))
				, Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/value[@name='testval']");
			Assert.That(gtestNode, Is.Not.Null);
			Assert.That(gtestNode.InnerText, Is.EqualTo("testVALUE"));
		}

		[Test]
		public void SetSetting_MustSaveInUserXML()
		{
			// Load configuration
			Configuration.Load();

			// Save the setting
			Assert.That(
				Configuration
					.Value("testval")
					.OfCategory("testcat")
					.InGroup("testgroup")
					.Set(new ConfigData().FromString("testVALUE"))
				, Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/value[@name='testval']");
			Assert.That(utestNode, Is.Not.Null);
			Assert.That(utestNode.InnerText, Is.EqualTo("testVALUE"));
		}

		[Test]
		public void SetListSetting_MustSaveInGlobalXML()
		{
			// Load configuration
			Configuration.Load();

			// Save the setting
			List<ConfigData> testlist = new List<ConfigData>();
			testlist.Add(new ConfigData().FromString("List1"));
			testlist.Add(new ConfigData().FromString("List2"));
			testlist.Add(new ConfigData().FromString("List3"));
			Assert.That(
				Configuration
					.List("testlist")
					.OfCategory("testcat")
					.InGroup("testgroup")
					.SetGlobal(testlist)
				, Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/list[@name='testlist']");
			Assert.That(gtestNode, Is.Not.Null);
			XmlNode gtestList1 = gtestNode.SelectSingleNode("value[1]");
			Assert.That(gtestList1.InnerText, Is.EqualTo("List1"));
			XmlNode gtestList2 = gtestNode.SelectSingleNode("value[2]");
			Assert.That(gtestList2.InnerText, Is.EqualTo("List2"));
			XmlNode gtestList3 = gtestNode.SelectSingleNode("value[3]");
			Assert.That(gtestList3.InnerText, Is.EqualTo("List3"));
		}

		[Test]
		public void SetListSetting_MustSaveInUserXML()
		{
			// Load configuration
			Configuration.Load();

			// Save the setting
			List<ConfigData> testlist = new List<ConfigData>();
			testlist.Add(new ConfigData().FromString("List1"));
			testlist.Add(new ConfigData().FromString("List2"));
			testlist.Add(new ConfigData().FromString("List3"));
			Assert.That(
				Configuration
					.List("testlist")
					.OfCategory("testcat")
					.InGroup("testgroup")
					.Set(testlist)
				, Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/list[@name='testlist']");
			Assert.That(utestNode, Is.Not.Null);
			XmlNode utestList1 = utestNode.SelectSingleNode("value[1]");
			Assert.That(utestList1.InnerText, Is.EqualTo("List1"));
			XmlNode utestList2 = utestNode.SelectSingleNode("value[2]");
			Assert.That(utestList2.InnerText, Is.EqualTo("List2"));
			XmlNode utestList3 = utestNode.SelectSingleNode("value[3]");
			Assert.That(utestList3.InnerText, Is.EqualTo("List3"));
		}

		[Test]
		public void RemoveSetting_MustReturnFailureIfNotFound()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the setting
			Assert.That(Configuration.Value("testval").OfCategory("testcat____").InGroup("testgroup").RemoveGlobal(),
				Is.False);
			Assert.That(Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup____").RemoveGlobal(),
				Is.False);
			Assert.That(Configuration.Value("testval____").OfCategory("testcat").InGroup("testgroup").RemoveGlobal(),
				Is.False);
		}

		[Test]
		public void RemoveSetting_MustDeleteFromGlobalXMLAndLeaveInUserXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "testval");
			ugroupNode.Add(uvalueNode);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the setting
			Assert.That(Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").RemoveGlobal(),
				Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']");
			Assert.That(gtestNode, Is.Null);

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/value[@name='testval']");
			Assert.That(utestNode, Is.Not.Null);
		}

		[Test]
		public void RemoveSetting_MustDeleteFromUserXMLAndLeaveInGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "testval");
			ugroupNode.Add(uvalueNode);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the setting
			Assert.That(Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").Remove(),
				Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/value[@name='testval']");
			Assert.That(gtestNode, Is.Not.Null);

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']");
			Assert.That(utestNode, Is.Null);
		}

		[Test]
		public void RemoveListSetting_MustReturnFailureIfNotFound()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.Value = "List1";
			glistNode.Add(gvalueNode1);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.Value = "List2";
			glistNode.Add(gvalueNode2);
			XElement gvalueNode3 = new XElement("value");
			gvalueNode3.Value = "List3";
			glistNode.Add(gvalueNode3);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the setting
			Assert.That(Configuration.List("testlist").OfCategory("testcat____").InGroup("testgroup").RemoveGlobal(),
				Is.False);
			Assert.That(Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup____").RemoveGlobal(),
				Is.False);
			Assert.That(Configuration.List("testlist____").OfCategory("testcat").InGroup("testgroup").RemoveGlobal(),
				Is.False);
		}

		[Test]
		public void RemoveListSetting_MustDeleteFromGlobalXMLAndLeaveInUserXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.Value = "List1";
			glistNode.Add(gvalueNode1);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.Value = "List2";
			glistNode.Add(gvalueNode2);
			XElement gvalueNode3 = new XElement("value");
			gvalueNode3.Value = "List3";
			glistNode.Add(gvalueNode3);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement ulistNode = new XElement("list");
			ulistNode.SetAttributeValue("name", "testlist");
			ugroupNode.Add(ulistNode);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.Value = "userList1";
			ulistNode.Add(uvalueNode1);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.Value = "userList2";
			ulistNode.Add(uvalueNode2);
			XElement uvalueNode3 = new XElement("value");
			uvalueNode3.Value = "userList3";
			ulistNode.Add(uvalueNode3);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the setting
			Assert.That(Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup").RemoveGlobal(),
				Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']");
			Assert.That(gtestNode, Is.Null);

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/list[@name='testlist']");
			Assert.That(utestNode, Is.Not.Null);
		}

		[Test]
		public void RemoveListSetting_MustDeleteFromUserXMLAndLeaveInGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.Value = "List1";
			glistNode.Add(gvalueNode1);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.Value = "List2";
			glistNode.Add(gvalueNode2);
			XElement gvalueNode3 = new XElement("value");
			gvalueNode3.Value = "List3";
			glistNode.Add(gvalueNode3);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement ulistNode = new XElement("list");
			ulistNode.SetAttributeValue("name", "testlist");
			ugroupNode.Add(ulistNode);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.Value = "userList1";
			ulistNode.Add(uvalueNode1);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.Value = "userList2";
			ulistNode.Add(uvalueNode2);
			XElement uvalueNode3 = new XElement("value");
			uvalueNode3.Value = "userList3";
			ulistNode.Add(uvalueNode3);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the setting
			Assert.That(Configuration.List("testlist").OfCategory("testcat").InGroup("testgroup").Remove(),
				Is.True);

			// Now save the configuration and test file contents
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']/list[@name='testlist']");
			Assert.That(gtestNode, Is.Not.Null);

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']");
			Assert.That(utestNode, Is.Null);
		}

		[Test]
		public void GetSettingList_MustReadFromGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			ggroupNode.Add(gvalueNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement glistValueNode1 = new XElement("value");
			glistValueNode1.Value = "List1";
			glistNode.Add(glistValueNode1);
			XElement glistValueNode2 = new XElement("value");
			glistValueNode2.Value = "List2";
			glistNode.Add(glistValueNode2);
			XElement glistValueNode3 = new XElement("value");
			glistValueNode3.Value = "List3";
			glistNode.Add(glistValueNode3);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Check return values of GetSettingList()
			string[] settingListAll = Configuration.Group("testgroup").OfCategory("testcat").AllSettings;
			Assert.That(settingListAll, Is.Not.Null);
			Assert.That(settingListAll.Length, Is.EqualTo(2));
			Assert.That(settingListAll, Contains.Item("testval"));
			Assert.That(settingListAll, Contains.Item("testlist"));

			string[] settingListValues = Configuration.Group("testgroup").OfCategory("testcat").Values;
			Assert.That(settingListValues, Is.Not.Null);
			Assert.That(settingListValues.Length, Is.EqualTo(1));
			Assert.That(settingListValues, Contains.Item("testval"));

			string[] settingListList = Configuration.Group("testgroup").OfCategory("testcat").Lists;
			Assert.That(settingListList, Is.Not.Null);
			Assert.That(settingListList.Length, Is.EqualTo(1));
			Assert.That(settingListList, Contains.Item("testlist"));
		}

		[Test]
		public void GetSettingList_MustReadFromUserXML()
		{
			// Prepare test files
			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "testval");
			ugroupNode.Add(uvalueNode);
			XElement ulistNode = new XElement("list");
			ulistNode.SetAttributeValue("name", "testlist");
			ugroupNode.Add(ulistNode);
			XElement ulistValueNode1 = new XElement("value");
			ulistValueNode1.Value = "List1";
			ulistNode.Add(ulistValueNode1);
			XElement ulistValueNode2 = new XElement("value");
			ulistValueNode2.Value = "List2";
			ulistNode.Add(ulistValueNode2);
			XElement ulistValueNode3 = new XElement("value");
			ulistValueNode3.Value = "List3";
			ulistNode.Add(ulistValueNode3);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Check return values of GetSettingList()
			string[] settingListAll = Configuration.Group("testgroup").OfCategory("testcat").AllSettings;
			Assert.That(settingListAll, Is.Not.Null);
			Assert.That(settingListAll.Length, Is.EqualTo(2));
			Assert.That(settingListAll, Contains.Item("testval"));
			Assert.That(settingListAll, Contains.Item("testlist"));

			string[] settingListValues = Configuration.Group("testgroup").OfCategory("testcat").Values;
			Assert.That(settingListValues, Is.Not.Null);
			Assert.That(settingListValues.Length, Is.EqualTo(1));
			Assert.That(settingListValues, Contains.Item("testval"));

			string[] settingListList = Configuration.Group("testgroup").OfCategory("testcat").Lists;
			Assert.That(settingListList, Is.Not.Null);
			Assert.That(settingListList.Length, Is.EqualTo(1));
			Assert.That(settingListList, Contains.Item("testlist"));
		}

		[Test]
		public void GetSettingList_MustReadFromMergedXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			ggroupNode.Add(gvalueNode);
			XElement glistNode = new XElement("list");
			glistNode.SetAttributeValue("name", "testlist");
			ggroupNode.Add(glistNode);
			XElement glistValueNode1 = new XElement("value");
			glistValueNode1.Value = "List1";
			glistNode.Add(glistValueNode1);
			XElement glistValueNode2 = new XElement("value");
			glistValueNode2.Value = "List2";
			glistNode.Add(glistValueNode2);
			XElement glistValueNode3 = new XElement("value");
			glistValueNode3.Value = "List3";
			glistNode.Add(glistValueNode3);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "USER_testval");
			ugroupNode.Add(uvalueNode);
			XElement ulistNode = new XElement("list");
			ulistNode.SetAttributeValue("name", "USER_testlist");
			ugroupNode.Add(ulistNode);
			XElement ulistValueNode1 = new XElement("value");
			ulistValueNode1.Value = "List1";
			ulistNode.Add(ulistValueNode1);
			XElement ulistValueNode2 = new XElement("value");
			ulistValueNode2.Value = "List2";
			ulistNode.Add(ulistValueNode2);
			XElement ulistValueNode3 = new XElement("value");
			ulistValueNode3.Value = "List3";
			ulistNode.Add(ulistValueNode3);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Check return values of GetSettingList()
			string[] settingListAll = Configuration.Group("testgroup").OfCategory("testcat").AllSettings;
			Assert.That(settingListAll, Is.Not.Null);
			Assert.That(settingListAll.Length, Is.EqualTo(4));
			Assert.That(settingListAll, Contains.Item("testval"));
			Assert.That(settingListAll, Contains.Item("testlist"));
			Assert.That(settingListAll, Contains.Item("USER_testval"));
			Assert.That(settingListAll, Contains.Item("USER_testlist"));

			string[] settingListValues = Configuration.Group("testgroup").OfCategory("testcat").Values;
			Assert.That(settingListValues, Is.Not.Null);
			Assert.That(settingListValues.Length, Is.EqualTo(2));
			Assert.That(settingListValues, Contains.Item("testval"));
			Assert.That(settingListValues, Contains.Item("USER_testval"));

			string[] settingListList = Configuration.Group("testgroup").OfCategory("testcat").Lists;
			Assert.That(settingListList, Is.Not.Null);
			Assert.That(settingListList.Length, Is.EqualTo(2));
			Assert.That(settingListList, Contains.Item("testlist"));
			Assert.That(settingListList, Contains.Item("USER_testlist"));
		}

		[Test]
		public void GetGroupList_MustReadFromGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode1 = new XElement("group");
			ggroupNode1.SetAttributeValue("name", "testgroup1");
			gcategoryNode.Add(ggroupNode1);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.SetAttributeValue("name", "testval1");
			ggroupNode1.Add(gvalueNode1);
			XElement ggroupNode2 = new XElement("group");
			ggroupNode2.SetAttributeValue("name", "testgroup2");
			gcategoryNode.Add(ggroupNode2);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.SetAttributeValue("name", "testval2");
			ggroupNode2.Add(gvalueNode2);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Test result of GetGroupList() method
			string[] groupList = Configuration.GroupsOf("testcat");
			Assert.That(groupList, Is.Not.Null);
			Assert.That(groupList.Length, Is.EqualTo(2));
			Assert.That(groupList, Contains.Item("testgroup1"));
			Assert.That(groupList, Contains.Item("testgroup2"));
		}

		[Test]
		public void GetGroupList_MustReadFromUserXML()
		{
			// Prepare test files
			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode1 = new XElement("group");
			ugroupNode1.SetAttributeValue("name", "testgroup1");
			ucategoryNode.Add(ugroupNode1);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.SetAttributeValue("name", "testval1");
			ugroupNode1.Add(uvalueNode1);
			XElement ugroupNode2 = new XElement("group");
			ugroupNode2.SetAttributeValue("name", "testgroup2");
			ucategoryNode.Add(ugroupNode2);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.SetAttributeValue("name", "testval2");
			ugroupNode2.Add(uvalueNode2);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test result of GetGroupList() method
			string[] groupList = Configuration.GroupsOf("testcat");
			Assert.That(groupList, Is.Not.Null);
			Assert.That(groupList.Length, Is.EqualTo(2));
			Assert.That(groupList, Contains.Item("testgroup1"));
			Assert.That(groupList, Contains.Item("testgroup2"));
		}

		[Test]
		public void GetGroupList_MustReadFromMergedXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode1 = new XElement("group");
			ggroupNode1.SetAttributeValue("name", "testgroup1");
			gcategoryNode.Add(ggroupNode1);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.SetAttributeValue("name", "testval1");
			ggroupNode1.Add(gvalueNode1);
			XElement ggroupNode2 = new XElement("group");
			ggroupNode2.SetAttributeValue("name", "testgroup2");
			gcategoryNode.Add(ggroupNode2);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.SetAttributeValue("name", "testval2");
			ggroupNode2.Add(gvalueNode2);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode1 = new XElement("group");
			ugroupNode1.SetAttributeValue("name", "testgroup2");
			ucategoryNode.Add(ugroupNode1);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.SetAttributeValue("name", "testval3");
			ugroupNode1.Add(uvalueNode1);
			XElement ugroupNode2 = new XElement("group");
			ugroupNode2.SetAttributeValue("name", "testgroup4");
			ucategoryNode.Add(ugroupNode2);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.SetAttributeValue("name", "testval4");
			ugroupNode2.Add(uvalueNode2);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test result of GetGroupList() method
			string[] groupList = Configuration.GroupsOf("testcat");
			Assert.That(groupList, Is.Not.Null);
			Assert.That(groupList.Length, Is.EqualTo(3));
			Assert.That(groupList, Contains.Item("testgroup1"));
			Assert.That(groupList, Contains.Item("testgroup2"));
			Assert.That(groupList, Contains.Item("testgroup4"));
		}

		[Test]
		public void GetCategoryList_MustReadFromGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode1 = new XElement("category");
			gcategoryNode1.SetAttributeValue("name", "testcat1");
			groot.Add(gcategoryNode1);
			XElement ggroupNode1 = new XElement("group");
			ggroupNode1.SetAttributeValue("name", "testgroup1");
			gcategoryNode1.Add(ggroupNode1);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.SetAttributeValue("name", "testval1");
			ggroupNode1.Add(gvalueNode1);
			XElement gcategoryNode2 = new XElement("category");
			gcategoryNode2.SetAttributeValue("name", "testcat2");
			groot.Add(gcategoryNode2);
			XElement ggroupNode2 = new XElement("group");
			ggroupNode2.SetAttributeValue("name", "testgroup2");
			gcategoryNode2.Add(ggroupNode2);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.SetAttributeValue("name", "testval2");
			ggroupNode2.Add(gvalueNode2);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Test result of GetCategoryList() method
			string[] categoryList = Configuration.Categories;
			Assert.That(categoryList, Is.Not.Null);
			Assert.That(categoryList.Length, Is.EqualTo(2));
			Assert.That(categoryList, Contains.Item("testcat1"));
			Assert.That(categoryList, Contains.Item("testcat2"));
		}

		[Test]
		public void GetCategoryList_MustReadFromUserXML()
		{
			// Prepare test files
			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode1 = new XElement("category");
			ucategoryNode1.SetAttributeValue("name", "testcat1");
			uroot.Add(ucategoryNode1);
			XElement ugroupNode1 = new XElement("group");
			ugroupNode1.SetAttributeValue("name", "testgroup1");
			ucategoryNode1.Add(ugroupNode1);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.SetAttributeValue("name", "testval1");
			ugroupNode1.Add(uvalueNode1);
			XElement ucategoryNode2 = new XElement("category");
			ucategoryNode2.SetAttributeValue("name", "testcat2");
			uroot.Add(ucategoryNode2);
			XElement ugroupNode2 = new XElement("group");
			ugroupNode2.SetAttributeValue("name", "testgroup2");
			ucategoryNode2.Add(ugroupNode2);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.SetAttributeValue("name", "testval2");
			ugroupNode2.Add(uvalueNode2);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test result of GetCategoryList() method
			string[] categoryList = Configuration.Categories;
			Assert.That(categoryList, Is.Not.Null);
			Assert.That(categoryList.Length, Is.EqualTo(2));
			Assert.That(categoryList, Contains.Item("testcat1"));
			Assert.That(categoryList, Contains.Item("testcat2"));
		}

		[Test]
		public void GetCategoryList_MustReadFromMergedXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode1 = new XElement("category");
			gcategoryNode1.SetAttributeValue("name", "testcat1");
			groot.Add(gcategoryNode1);
			XElement ggroupNode1 = new XElement("group");
			ggroupNode1.SetAttributeValue("name", "testgroup1");
			gcategoryNode1.Add(ggroupNode1);
			XElement gvalueNode1 = new XElement("value");
			gvalueNode1.SetAttributeValue("name", "testval1");
			ggroupNode1.Add(gvalueNode1);
			XElement gcategoryNode2 = new XElement("category");
			gcategoryNode2.SetAttributeValue("name", "testcat2");
			groot.Add(gcategoryNode2);
			XElement ggroupNode2 = new XElement("group");
			ggroupNode2.SetAttributeValue("name", "testgroup2");
			gcategoryNode2.Add(ggroupNode2);
			XElement gvalueNode2 = new XElement("value");
			gvalueNode2.SetAttributeValue("name", "testval2");
			ggroupNode2.Add(gvalueNode2);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode1 = new XElement("category");
			ucategoryNode1.SetAttributeValue("name", "testcat2");
			uroot.Add(ucategoryNode1);
			XElement ugroupNode1 = new XElement("group");
			ugroupNode1.SetAttributeValue("name", "testgroup3");
			ucategoryNode1.Add(ugroupNode1);
			XElement uvalueNode1 = new XElement("value");
			uvalueNode1.SetAttributeValue("name", "testval3");
			ugroupNode1.Add(uvalueNode1);
			XElement ucategoryNode2 = new XElement("category");
			ucategoryNode2.SetAttributeValue("name", "testcat4");
			uroot.Add(ucategoryNode2);
			XElement ugroupNode2 = new XElement("group");
			ugroupNode2.SetAttributeValue("name", "testgroup4");
			ucategoryNode2.Add(ugroupNode2);
			XElement uvalueNode2 = new XElement("value");
			uvalueNode2.SetAttributeValue("name", "testval4");
			ugroupNode2.Add(uvalueNode2);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test result of GetCategoryList() method
			string[] categoryList = Configuration.Categories;
			Assert.That(categoryList, Is.Not.Null);
			Assert.That(categoryList.Length, Is.EqualTo(3));
			Assert.That(categoryList, Contains.Item("testcat1"));
			Assert.That(categoryList, Contains.Item("testcat2"));
			Assert.That(categoryList, Contains.Item("testcat4"));
		}

		[Test]
		public void RenameGroup_MustRenameInGlobalAndUserConfiguration()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			gvalueNode.Value = "A_Value";
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "testval");
			uvalueNode.Value = "userTESTVAL";
			ugroupNode.Add(uvalueNode);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Test renaming for user group
			ConfigData testval = Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").Get;
			Assert.That(testval, Is.Not.Null);
			Assert.That(testval.AsString(), Is.EqualTo("userTESTVAL"));

			Assert.That(Configuration.Group("testgroup").OfCategory("testcat").RenameTo("testgroup2"), Is.True);

			// Now there must be the old group in global configuration and the new one in user's configuration
			testval = Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup").Get;
			Assert.That(testval, Is.Not.Null);
			Assert.That(testval.AsString(), Is.EqualTo("A_Value"));
			testval = Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup2").Get;
			Assert.That(testval, Is.Not.Null);
			Assert.That(testval.AsString(), Is.EqualTo("userTESTVAL"));

			// Test renaming for global group
			Assert.That(Configuration.Group("testgroup").OfCategory("testcat").RenameGloballyTo("testgroup3"), Is.True);
			testval = Configuration.Value("testval").OfCategory("testcat").InGroup("testgroup3").Get;
			Assert.That(testval, Is.Not.Null);
			Assert.That(testval.AsString(), Is.EqualTo("A_Value"));
		}

		[Test]
		public void RemoveGroup_MustReturnFailureIfNotFound()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			gvalueNode.Value = "A_Value";
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the group and save the configuration
			Assert.That(Configuration.Group("testgroup").OfCategory("testcat____").RemoveGlobally(), Is.False);
			Assert.That(Configuration.Group("testgroup____").OfCategory("testcat").RemoveGlobally(), Is.False);
		}

		[Test]
		public void RemoveGroup_MustRemoveFromGlobalXML()
		{
			// Prepare test files
			XElement groot = new XElement("NQConfiguration");
			XDocument gdoc = new XDocument(groot);
			groot.SetAttributeValue("version", "1.0");
			XElement gcategoryNode = new XElement("category");
			gcategoryNode.SetAttributeValue("name", "testcat");
			groot.Add(gcategoryNode);
			XElement ggroupNode = new XElement("group");
			ggroupNode.SetAttributeValue("name", "testgroup");
			gcategoryNode.Add(ggroupNode);
			XElement gvalueNode = new XElement("value");
			gvalueNode.SetAttributeValue("name", "testval");
			gvalueNode.Value = "A_Value";
			ggroupNode.Add(gvalueNode);
			gdoc.Save(_globalConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the group and save the configuration
			Assert.That(Configuration.Group("testgroup").OfCategory("testcat").RemoveGlobally(), Is.True);
			Configuration.Save();

			XmlDocument gsavedDoc = new XmlDocument();
			gsavedDoc.Load(_globalConfigFile);
			XmlNode gtestNode =
				gsavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']");
			Assert.That(gtestNode, Is.Null);
		}

		[Test]
		public void RemoveGroup_MustRemoveFromUserXML()
		{
			// Prepare test files
			XElement uroot = new XElement("NQConfiguration");
			XDocument udoc = new XDocument(uroot);
			uroot.SetAttributeValue("version", "1.0");
			XElement ucategoryNode = new XElement("category");
			ucategoryNode.SetAttributeValue("name", "testcat");
			uroot.Add(ucategoryNode);
			XElement ugroupNode = new XElement("group");
			ugroupNode.SetAttributeValue("name", "testgroup");
			ucategoryNode.Add(ugroupNode);
			XElement uvalueNode = new XElement("value");
			uvalueNode.SetAttributeValue("name", "testval");
			uvalueNode.Value = "A_Value";
			ugroupNode.Add(uvalueNode);
			udoc.Save(_userConfigFile);

			// Load configuration
			Configuration.Load();

			// Remove the group and save the configuration
			Assert.That(Configuration.Group("testgroup").OfCategory("testcat").Remove(), Is.True);
			Configuration.Save();

			XmlDocument usavedDoc = new XmlDocument();
			usavedDoc.Load(_userConfigFile);
			XmlNode utestNode =
				usavedDoc.SelectSingleNode("/NQConfiguration/category[@name='testcat']/group[@name='testgroup']");
			Assert.That(utestNode, Is.Null);
		}
	}
}
